package com.ztgf.commodity.api.cms;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.commodity.models.bo.CommodityUnitBO;
import com.ztgf.commodity.models.bo.CommodityUnitSaveBO;
import com.ztgf.commodity.models.dto.CommodityUnitDTO;
import com.ztgf.commodity.models.enums.EPGCodeEnum;
import com.ztgf.commodity.models.service.ICommodityUnitService;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.common.utils.StrUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * author: chenyu.wang
 * date: 2020-05-26 11:58
 * Description:
 */
@RestController
@RequestMapping("/cms/commodityUnit")
@Slf4j
@Api(value = "CommodityUnitController", tags = "商品单位表接口")
public class CommodityUnitController {

    @DubboReference
    private ICommodityUnitService commodityUnitService;

    @PostMapping(value = "/insert")
    @ApiOperation(value = "添加商品单位表", notes = "添加商品单位表")
    public Result insertCommodityUnit(CommodityUnitSaveBO saveBO) {
        return commodityUnitService.add(saveBO);
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改商品单位表", notes = "修改商品单位表")
    public Result updateCommodityUnit(CommodityUnitSaveBO saveBO) {
        CommodityUnitDTO dto = new CommodityUnitDTO();
        BeanUtils.copyProperties(saveBO, dto);
        dto.setId(saveBO.getId());
        boolean flag = commodityUnitService.updateById(dto);
        if (flag) {
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }

    @ApiOperation(value = "删除商品单位表(物理删除)", notes = "删除商品单位表(物理删除)")
    @ApiImplicitParam(name = "id", value = "商品单位表id", required = true, paramType = "query", dataType = "Long")
    @PostMapping("/delete")
    public Result delete(Long id) {
        if (null == id) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        boolean flag = commodityUnitService.deleteById(id);
        if (flag) {
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100009);
        }
    }

    @ApiOperation(value = "删除/恢复商品单位表(逻辑删除)", notes = "删除/恢复商品单位表(逻辑删除)")
    @ApiImplicitParam(name = "id", value = "商品单位表id", required = true, paramType = "query", dataType = "Long")
    @PostMapping("/deleteLogic")
    public Result deleteLogic(Long id) {
        if (null == id) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        boolean flag = commodityUnitService.deleteLogic(id);
        if (flag) {
            return new Result<>();
        }
        return new Result<>(CodeEnum.FAIL_100009);
    }

    @PostMapping("/deleteBatch")
    @ApiOperation(value = "批量删除商品单位表", notes = "批量删除商品单位表")
    @ApiImplicitParam(name = "ids", value = "ids:以逗号隔开", required = true, paramType = "query", dataType = "String")
    public Result deleteBatch(String ids) {
        if (StrUtils.isEmpty(ids)) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        String[] arr = ids.split(",");
        commodityUnitService.deleteBatchIds(Arrays.asList(arr));
        return new Result<>();
    }

    @PostMapping("/getById")
    @ApiOperation(value = "根据id查找商品单位表", notes = "根据id查找商品单位表")
    @ApiImplicitParam(name = "id", value = "商品单位表id", required = true, paramType = "query", dataType = "Long")
    public Result<CommodityUnitDTO> getById(Long id) {
        if (null == id) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        CommodityUnitDTO dto = commodityUnitService.selectById(id);
        return new Result<>(dto);
    }

    @ApiOperation(value = "商品单位列表", notes = "商品单位列表")
    @PostMapping("/queryPageList")
    public Result<Page<CommodityUnitDTO>> queryPageList(CommodityUnitBO bo) {
        Wrapper<CommodityUnitDTO> wrapper = new EntityWrapper<>();
        if (bo.getName() != null) {
            wrapper.like("name", bo.getName());
        }
        if (bo.getCode() != null) {
            wrapper.like("code", bo.getCode());
        }
        if (bo.getIsPublish() != null) {
            wrapper.eq("is_publish", bo.getIsPublish());
        }
        if (bo.getStatus() != null) {
            wrapper.eq("status", bo.getStatus());
        }
        wrapper.orderBy("create_time", false);
        Page<CommodityUnitDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = commodityUnitService.selectPage(page, wrapper);
        return new Result<>(page);
    }

    @ApiOperation(value = "商品单位列表排序", notes = "商品单位列表排序")
    @PostMapping("/queryPageOrderList")
    public Result<Page<CommodityUnitDTO>> queryPageBySortList(CommodityUnitBO bo) {
        Wrapper<CommodityUnitDTO> wrapper = new EntityWrapper<>();
        Result result = new Result();
        if (null == bo.getSortType()) {
            result.setCode(CodeEnum.FAIL.getCode());
            result.setData("传入排序规则为空");
            return result;
        }
        if (bo.getSortType().equals(EPGCodeEnum.SORT_ASC.getCode())) {
            wrapper.orderBy("sort", true);
        } else {
            wrapper.orderBy("sort", false);
        }
        Page<CommodityUnitDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = commodityUnitService.selectPage(page);
        return new Result<>(page);
    }
}
